﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace dzs.Web.supply
{
    public partial class ListTotalSupplyStatus : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDDL();
                Bind();

            }
        }


        private void BindDDL()
        {
            string str = " roles_id = 6 ";

            DataSet ds = new dzs.BLL.user().GetList(str);

            this.ddl_gygly.DataSource = ds;
            this.ddl_gygly.DataTextField = "user_name";
            this.ddl_gygly.DataValueField = "user_id";
            this.ddl_gygly.DataBind();

            this.ddl_gygly.Items.Insert(0, new ListItem("--未选择--","-1"));

        }

        private void Bind()
        {
            if (this.ddl_status.SelectedValue == "-1")
            {
                this.GridView2.Visible = false;
                this.GridView1.Visible = false;
                this.lbl_complate.Visible = false;
                this.lbl_dsCount.Visible = false;

            }
            else if(this.ddl_status.SelectedValue == "已生成月供应计划")
            {
                GridView2Bind();
                this.GridView2.Visible = true;
                this.GridView1.Visible = false;
                this.lbl_complate.Visible = true;
                this.lbl_dsCount.Visible = true;
            }
            else
            {
                GridView1Bind();
                this.GridView2.Visible = false;
                this.GridView1.Visible = true;
                this.lbl_complate.Visible = true;
                this.lbl_dsCount.Visible = true;
            }

           
        }
        private void GridView2Bind()
        {
            Maticsoft.BLL.monthSupplayPlan msm = new Maticsoft.BLL.monthSupplayPlan();
            Maticsoft.BLL.supplyPlan sm = new Maticsoft.BLL.supplyPlan();

            List<dzs.Model.configinfo> cm = new dzs.BLL.configinfo().GetModelList("");//获取时间结算日期
            dzs.Model.configinfo c = cm[0];

            string user_id = this.ddl_gygly.SelectedItem.Value;
            string str = " 1=1 ";

            if (user_id != "-1")
            {
                str += " and user_id = " + user_id;
            }

            if (!string.IsNullOrEmpty(this.txt_supplyDate.Text))
            {
                DateTime ghq = DateTime.Parse(this.txt_supplyDate.Text);
                #region 月供应计划起始结束时间

                DateTime monthEndTime;//本月供应计划结束时间
                DateTime monthStartTime;//本月供应计划起始时间


                int day = ghq.Day;

                if (day <= c.ygyjs)
                {
                    monthStartTime = DateTime.Parse(ghq.AddMonths(-1).ToString("yyyy-MM-" + c.ygyqs + ""));
                    monthEndTime = DateTime.Parse(ghq.ToString("yyyy-MM-" + c.ygyjs + ""));
                }
                else
                {
                    monthStartTime = DateTime.Parse(ghq.ToString("yyyy-MM-" + c.ygyqs + ""));
                    monthEndTime = DateTime.Parse(ghq.AddMonths(1).ToString("yyyy-MM-" + c.ygyjs + ""));
                }
                #endregion

                str += " and supplyDate between '" + monthStartTime + "' and '" + monthEndTime + "'  ";//此处的supplyDate为supplyPlan中的供货期，不是供应年月
            }

            DataSet ds_monthSupply = msm.GetListMonthSupplyTotalCount(str);
            DataSet ds_supply = sm.GetList(str);

            int monthSupplyCount = 0;//生成的月供应计划数量
            int supplyCount = 0;//供应计划总数
            decimal present = 0;//当月计划生成及时率

            if (ds_monthSupply.Tables[0].Rows.Count > 0)
            {
                monthSupplyCount = ds_monthSupply.Tables[0].Rows.Count;
            }
            if (ds_supply.Tables[0].Rows.Count > 0)
            {
                supplyCount = ds_supply.Tables[0].Rows.Count;
            }

            if (supplyCount != 0)
            {
                present = decimal.Divide(monthSupplyCount, supplyCount);
                present = decimal.Round(present, 2) * 100;
                this.lbl_complate.Text = "已生成的月供应计划数：" + monthSupplyCount + "；当月供应计划总数：" + supplyCount + "；供应计划完成率为：" + present + "%。";
            }

            this.GridView2.DataSource = ds_monthSupply;
            this.GridView2.DataBind();
            this.lbl_dsCount.Text = "当前共 " + ds_monthSupply.Tables[0].Rows.Count + " 条数据";
            this.lbl_supplyDateWarn.Text = "*月供应计划起始日期为上月 " + c.ygyqs + " 号至当月 " + c.ygyjs + " 号";
        }
        private void GridView1Bind()
        {
            DataSet ds = null;
            Maticsoft.BLL.supplyPlan sm = new Maticsoft.BLL.supplyPlan();

            List<dzs.Model.configinfo> cm = new dzs.BLL.configinfo().GetModelList("");//获取时间结算日期
            dzs.Model.configinfo c = cm[0];

            string user_id = this.ddl_gygly.SelectedItem.Value;
            string str = " 1=1 ";

            if (user_id != "-1")
            {
                str += " and user_id = " + user_id;
            }

            if (!string.IsNullOrEmpty(this.txt_supplyDate.Text))
            {
                DateTime ghq = DateTime.Parse(this.txt_supplyDate.Text);

                #region 月供应计划起始结束时间
                DateTime monthEndTime;//本月供应计划结束时间
                DateTime monthStartTime;//本月供应计划起始时间

                int day = ghq.Day;

                if (day <= c.ygyjs)
                {
                    monthStartTime = DateTime.Parse(ghq.AddMonths(-1).ToString("yyyy-MM-" + c.ygyqs + ""));
                    monthEndTime = DateTime.Parse(ghq.ToString("yyyy-MM-" + c.ygyjs + ""));
                }
                else
                {
                    monthStartTime = DateTime.Parse(ghq.ToString("yyyy-MM-" + c.ygyqs + ""));
                    monthEndTime = DateTime.Parse(ghq.AddMonths(1).ToString("yyyy-MM-" + c.ygyjs + ""));
                }
                #endregion


                str += " and supplyDate between '" + monthStartTime + "' and '" + monthEndTime + "'  ";
            }

            ds = sm.GetList(str);

            DataTable dt_ywc = ds.Tables[0].Clone();//已完成计划
            DataTable dt_wwc = ds.Tables[0].Clone();//未完成计划

            int count = ds.Tables[0].Rows.Count;

            int complateCount = 0;

            foreach (DataRow row in ds.Tables[0].Rows)
            {
                decimal innums = 0;
                decimal num = 0;

                if (!string.IsNullOrEmpty(row["innums"].ToString()))
                {
                    innums = decimal.Parse(row["innums"].ToString());
                }
                if (!string.IsNullOrEmpty(row["num"].ToString()))
                {
                    num = decimal.Parse(row["num"].ToString());
                }
                if (innums == num)
                {
                    complateCount += 1;
                    dt_ywc.ImportRow(row);//已完成供应计划
                }
                else
                {
                    dt_wwc.ImportRow(row);//未完成供应计划
                }
            }

            if (count != 0)
            {
                decimal complate = decimal.Divide(complateCount, count);
                complate = decimal.Round(complate, 2);
                this.lbl_complate.Text = "供应计划数量：" + count + "；已完成计划数量：" + complateCount 
                    + "；未完成计划数量："+(count-complateCount)+"；供应计划完成率为：" + complate * 100 + "%。";

            }
            else
            {
                this.lbl_complate.Text = "";

            }
           
            this.lbl_supplyDateWarn.Text = "*月供应计划起始日期为上月 " + c.ygyqs + " 号至当月 " + c.ygyjs + " 号";

            if (this.ddl_status.SelectedValue == "已完成")
            {
                this.GridView1.DataSource = dt_ywc;
                this.GridView1.DataBind();
                this.lbl_dsCount.Text = "当前共 " + dt_ywc.Rows.Count + " 条数据";
            }
            if (this.ddl_status.SelectedValue == "未完成")
            {
                this.GridView1.DataSource = dt_wwc;
                this.GridView1.DataBind();
                this.lbl_dsCount.Text = "当前共 " + dt_wwc.Rows.Count + " 条数据";
            }
          
        }
        protected void btn_sel_Click(object sender, EventArgs e)
        {
            Bind();
        }

        protected void btn_reload_Click(object sender, EventArgs e)
        {
            Bind();
        }


        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;

            Bind();
        }

        //导出复写
        public override void VerifyRenderingInServerForm(Control control)       //导出
        { }
        //导出excel
        protected void btn_out_Click(object sender, EventArgs e)
        {
            if (this.ddl_status.SelectedValue != "-1")
            {
                if (this.ddl_status.SelectedValue == "已生成月供应计划")
                {
                    this.GridView2.AllowPaging = false;

                    Bind();

                    if (this.GridView2.Rows.Count != 0)
                    {
                        Response.Clear();
                        Response.Buffer = true;
                        Response.Charset = "GB2312";
                        Response.AppendHeader("Content-Disposition", "attachment;filename=yuegongjihua.xls");
                        //Response.ContentEncoding = System.Text.Encoding.UTF7;
                        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                        Response.ContentType = "application/ms-excel";
                        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
                        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
                        Response.Output.Write("<H4 align='center'>月供应计划状态统计</H4>");
                        this.GridView2.RenderControl(oHtmlTextWriter);
                        Response.Output.Write(oStringWriter.ToString());
                        Response.Flush();
                        Response.End();

                    }
                    else
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('数据为空，无法导出表格')</script>");

                    }
                }
                else
                {
                    this.GridView1.AllowPaging = false;

                    Bind();

                    if (this.GridView1.Rows.Count != 0)
                    {
                        Response.Clear();
                        Response.Buffer = true;
                        Response.Charset = "GB2312";
                        Response.AppendHeader("Content-Disposition", "attachment;filename=yuegongjihua.xls");
                        //Response.ContentEncoding = System.Text.Encoding.UTF7;
                        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                        Response.ContentType = "application/ms-excel";
                        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
                        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
                        Response.Output.Write("<H4 align='center'>月供应计划状态统计</H4>");
                        this.GridView1.RenderControl(oHtmlTextWriter);
                        Response.Output.Write(oStringWriter.ToString());
                        Response.Flush();
                        Response.End();

                    }
                    else
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('数据为空，无法导出表格')</script>");

                    }
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('数据为空，无法导出表格')</script>");
            }
           

        }

        //计划状态下拉列表框
        protected void ddl_status_SelectedIndexChanged(object sender, EventArgs e)
        {
            Bind();
        }
    }
}